package es.us.lsi.daw.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import es.us.lsi.daw.dao.DAOTwitterMySQL;
import es.us.lsi.daw.dao.mongo.DAOTwitter;
import es.us.lsi.daw.dto.DTODatoGrafica;
import es.us.lsi.daw.dto.DTOTweet;

@Controller
public class ControllerTwitter extends ControllerGenerico {

	// @Resource(name = "daoTwitter")
	@Autowired
	private DAOTwitter daoTwitter;
	
	@Autowired
	private DAOTwitterMySQL daoTwitterMySQL;

	@RequestMapping(value = PRIVADO + "graficaTweets.do", method = RequestMethod.GET)
	public String getGraficaTweets() {

		return "graficaTweets";
	}

	@RequestMapping(value = PRIVADO + "graficaTweetsAJAX.do", method = RequestMethod.POST)
	public ModelAndView getGraficaTweetsAjax(HttpServletRequest request,
			ModelMap modelo, HttpSession sesion,
			@RequestParam(value = "start_date") Long startDate,
			@RequestParam(value = "end_date") Long endDate) {

		List<DTODatoGrafica> lstDTODatoGrafica = daoTwitter
				.getLstDTODatoGrafica(startDate, endDate);
		List<DTODatoGrafica> lstDTODatoGrafica2 = daoTwitterMySQL
				.getLstDTODatoGrafica(startDate, endDate);
		
		modelo.addAttribute("lstDTODatoGrafica", lstDTODatoGrafica);
		return new ModelAndView("jsonView", modelo);
	}

	@RequestMapping(value = PRIVADO + "timeline.do", method = RequestMethod.GET)
	public String getTimeline() {

		return "timeline";
	}

	@RequestMapping(value = PRIVADO + "timeline.do", method = RequestMethod.POST)
	public ModelAndView getGraficaTweetsAjax(
			HttpServletRequest request,
			ModelMap modelo,
			HttpSession sesion,
			@RequestParam(value = "start_date") Long startDate,
			@RequestParam(value = "end_date") Long endDate,
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "num_elements", required = false, defaultValue = "10") Integer numElements) {

		List<DTOTweet> lstDTOTweet = daoTwitter.getLstDTOTweet(startDate,
				endDate, page, numElements);
		modelo.addAttribute("lstDTOTweet", lstDTOTweet);
		return new ModelAndView("jsonView", modelo);
	}

	@RequestMapping(value = PRIVADO + "search.do", method = RequestMethod.GET)
	public String getSearch() {

		return "search";
	}
	
	@RequestMapping(value = PRIVADO + "search.do", method = RequestMethod.GET)
	public ModelAndView postSearchAJAX(HttpServletRequest request,
			ModelMap modelo, HttpSession sesion,
			@RequestParam(value="authorQuery") String authorQuery,
			@RequestParam(value="textQuery") String textQuery) {

		List<DTOTweet> lstDTOTweet = daoTwitter.getLstDTOTweetByQuery(authorQuery,textQuery, 0, 10);
		modelo.addAttribute(lstDTOTweet);
		return new ModelAndView("jsonView", modelo);
	}
}
